import 'dart:convert';
import 'package:http/http.dart' as http;
final String url = 'https://httpbin.org/ip';

/* Future 异步函数 */
/* https://httpbin.org/ip */

/* 方式一 */
Future getIpAddress() {
  Uri uri = Uri.parse(url);
  return http.get(uri).then((resp) {
    final data = jsonDecode(resp.body);
    return data['origin'];
  });
}
/* 方式二 */
Future getIpAddress2() async {
  final String url = 'https://httpbin.org/ip';
  Uri uri = Uri.parse(url);
  final response = await http.get(uri);
  final data = jsonDecode(response.body);
  return data['origin'];
}
/* 用法一 */
void usage1() {
  getIpAddress()
      .then((ip) => print("ip -> $ip"))
      .catchError((err) => print(err));
}

/* 用法二 */
void usage2() async {
  try {
    final data = await getIpAddress();
    print("data -> $data");
  } catch (err) {
    print("err -> $err");
  }
}

void main() {
  usage2();
}